#include "includes/lesson.h"
#include <QList>
#include "includes/phrase.h"
#include <stdlib.h>
#include <QDebug>
#include <QTime>

Lesson::Lesson() : PhraseCollection()
{
}

void Lesson::fromXML(QString filename)
{
    QFile file(filename);
    file.open(QIODevice::ReadOnly);
    QDomDocument doc;

    QString error; int row, column;
    doc.setContent(&file, &error, &row, &column);

    difficulty = doc.elementsByTagName("lesson").at(0)
                 .toElement().attribute("difficulty").toInt();
    name = doc.elementsByTagName("name").at(0)
           .firstChild().nodeValue();
    description = doc.elementsByTagName("description").at(0)
                  .firstChild().nodeValue();
    icon = doc.elementsByTagName("icon").at(0)
           .firstChild().nodeValue();

    PhraseCollection::fromXML(filename, getName());
}

bool Lesson::equals(const Lesson &other) const
{
    return (this->name == other.getName());
}
bool Lesson::operator==(const Lesson& other) const
{ return this->equals(other); }

void Lesson::toXML(QString filename) const
{
    QFile file(filename);
    file.open(QIODevice::WriteOnly);
    QTextStream outputStream(&file);
    this->toXML(outputStream);
}

void Lesson::shuffle()
{
    srand(QTime().currentTime().msec());

    QList<QList<Phrase> > lists;

    for (int i = 0; i < 5; i++)
        lists.append(QList<Phrase>());

    for (int i = 0; i < this->size(); i++)
    {
        Phrase p = PhraseCollection::at(i);
        if ( rand() % 100 < 80)
            lists[p.getSequence()].append(p);
        else
            if (rand() % 100 < 50)
                if (p.getSequence() != 0)
                    lists[p.getSequence()-1].append(p);
                else lists[p.getSequence()].append(p);
            else
                if (p.getSequence() != 4)
                    lists[p.getSequence()+1].append(p);
                else lists[p.getSequence()].append(p);
    }

    QList<Phrase> temp;

    for (int i = 0; i < 5; i++)
    {
        while (lists.at(i).size() != 0)
        {
            int num = rand() % lists.at(i).size();
            temp.append(lists[i].takeAt(num));

            /*qDebug() << temp.last().getName() << " in list " << i
                    << " and originally in category " << temp.last().getSequence();*/
        }
    }

    this->clear();
    this->append(temp);
}


void Lesson::shuffle(int max)
{
    shuffle();
    qDebug() << max;
    while ( this->at(0).getSequence() < max ) this->removeAt(0);
}

void Lesson::toXML(QTextStream& stream) const
{
    QDomDocument outputNode("");
    this->toXML(outputNode);
    outputNode.save(stream,0);
}

QDomNode Lesson::toXML(QDomNode outputNode) const
{
    QDomDocument root = outputNode.ownerDocument();
    QDomElement node;

    node = root.createElement("lesson");
    outputNode.appendChild(node);
    node.setAttribute("difficulty", difficulty);

    QDomElement nameTag = root.createElement("name");
    nameTag.appendChild(root.createTextNode(name));
    node.appendChild(nameTag);

    QDomElement descTag = root.createElement("description");
    descTag.appendChild(root.createTextNode(description));
    node.appendChild(descTag);

    QDomElement iconTag = root.createElement("icon");
    iconTag.appendChild(root.createTextNode(icon));
    node.appendChild(iconTag);

    node.appendChild(PhraseCollection::toXML(outputNode));
    return root;
}

void Lesson::setName(QString n)
{ name = n; }

void Lesson::setIcon(QString i)
{ icon = i; }

void Lesson::setDescription(QString d)
{ description = d; }

void Lesson::setDifficulty(int d)
{ difficulty = d; }



QString Lesson::getName() const
{ return name; }

QString Lesson::getIcon() const
{ return icon; }

QString Lesson::getDescription() const
{ return description; }

int Lesson::getDifficulty() const
{ return difficulty; }

// equality operator
bool Lesson::operator==(const QString& other) const {
    return (getName() == other);
}
